{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### Introduction to Data Visualizations\n",
    "\n",
    "The Data Visualizations class `PlotlyViz` allows you to create and visualize different chart objects in Marquee.\n",
    "This class has been created to provide flexibility to our users when creating and visualizing charts. All while \n",
    "maintaining Marquee style and utilizing the proper Marquee workflow to persist and entitle these objects.\n",
    "\n",
    "At the moment we support 5 different chart types:\n",
    "\n",
    "    * Bar Charts\n",
    "    * Scatter Charts\n",
    "    * Bubble Charts\n",
    "    * Pie Charts\n",
    "    * Radar Charts\n",
    " \n",
    "Let's take a look at a few example and concepts to better understand how to work and create these visualizations.\n",
    "\n",
    "First, let's import all necessary libraries:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from gs_quant.analytics.datagrid import DataRow, DataColumn, DataGrid\n",
    "from gs_quant.analytics.dataviz import PlotlyViz\n",
    "from gs_quant.analytics.processors import EntityProcessor, VolatilityProcessor\n",
    "from gs_quant.analytics.processors.utility_processors import   LastProcessor, \\\n",
    "    DivisionProcessor\n",
    "from gs_quant.api.gs.users import GsUsersApi\n",
    "from gs_quant.data import DataCoordinate, DataMeasure, DataFrequency, DataDimension\n",
    "from gs_quant.datetime.relative_date import RelativeDate\n",
    "from gs_quant.entities.entitlements import Entitlements, EntitlementBlock, User\n",
    "from gs_quant.markets.securities import Asset, AssetIdentifier"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Next, let's create our Datagrid, all the visualization example we will be using the following [Datagrid](https://developer.gs.com/docs/gsquant/data/data-analytics/datagrid/overview/) \n",
    "as data source:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "text": [
      "h:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n",
      "h:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n",
      "h:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\nh:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\nh:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n",
      "h:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\nh:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\nh:\\virtual\\gs-quant\\lib\\site-packages\\urllib3\\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'marquee.web.gs.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n"
     ],
     "output_type": "stream"
    },
    {
     "name": "stdout",
     "text": [
      "\nDatagrid is currently experimental. The API is likely to change. For questions contact gs-marquee-markets@gs.com\n\n"
     ],
     "output_type": "stream"
    },
    {
     "data": {
      "text/plain": "Empty DataFrame\nColumns: []\nIndex: []",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 3
    }
   ],
   "source": [
    "# Fetching assets to work with\n",
    "SPX = Asset.get(\"MA4B66MW5E27U8P32SB\", AssetIdentifier.MARQUEE_ID)\n",
    "SX5E = Asset.get(\"MA4B66MW5E27U8P32SY\", AssetIdentifier.MARQUEE_ID)\n",
    "UKX = Asset.get(\"MA4B66MW5E27U8NN95P\", AssetIdentifier.MARQUEE_ID)\n",
    "HSCEI = Asset.get(\"MA4B66MW5E27U8P3294\", AssetIdentifier.MARQUEE_ID)\n",
    "NKY = Asset.get(\"MA4B66MW5E27U8P32LH\", AssetIdentifier.MARQUEE_ID)\n",
    "KS200 = Asset.get(\"MA4B66MW5E27U8P32DM\", AssetIdentifier.MARQUEE_ID)\n",
    "IBOV = Asset.get(\"MA4B66MW5E27U8NN8N6\", AssetIdentifier.MARQUEE_ID)\n",
    "\n",
    "# Defining Datagrid rows\n",
    "rows = [\n",
    "    DataRow(SPX),\n",
    "    DataRow(SX5E),\n",
    "    DataRow(UKX),\n",
    "    DataRow(HSCEI),\n",
    "    DataRow(NKY),\n",
    "    DataRow(KS200),\n",
    "    DataRow(IBOV)\n",
    "]\n",
    "\n",
    "# Defining column operations\n",
    "spx_spot = DataCoordinate(\n",
    "    measure=DataMeasure.CLOSE_PRICE,\n",
    "    frequency=DataFrequency.DAILY\n",
    ")\n",
    "\n",
    "implied_vol = DataCoordinate(\n",
    "    measure=DataMeasure.IMPLIED_VOLATILITY,\n",
    "    frequency=DataFrequency.DAILY,\n",
    "    dimensions={\n",
    "        DataDimension.TENOR: \"3m\",\n",
    "        DataDimension.STRIKE_REFERENCE: \"delta\",\n",
    "        DataDimension.RELATIVE_STRIKE: 0.5\n",
    "    }\n",
    ")\n",
    "\n",
    "implied_spot = LastProcessor(implied_vol)\n",
    "realized_vol = LastProcessor(\n",
    "    DivisionProcessor(VolatilityProcessor(spx_spot, w=63, start=RelativeDate(\"-1y\")), dividend=100)\n",
    ")\n",
    "\n",
    "col_0 = DataColumn(name=\"Name\", processor=EntityProcessor(field=\"short_name\"))\n",
    "col_1 = DataColumn(name=\"Implied Volatility\", processor=implied_spot)\n",
    "col_2 = DataColumn(name=\"Realized Vol\", processor=realized_vol)\n",
    "\n",
    "columns = [\n",
    "    col_0,\n",
    "    col_1,\n",
    "    col_2\n",
    "]\n",
    "\n",
    "# Entitlements\n",
    "# Entitle everyone internal to view but only yourself to edit and change entitlements\n",
    "user_id = GsUsersApi.get_my_guid()\n",
    "self_entitlement_block = EntitlementBlock(users=[User.get(user_id=user_id)])\n",
    "entitlements = Entitlements(view=self_entitlement_block, edit=self_entitlement_block, admin=self_entitlement_block)\n",
    "\n",
    "# Datagrid creation\n",
    "datagrid = DataGrid(\n",
    "    name=\"Datagrid For Visualization\",\n",
    "    rows=rows,\n",
    "    columns=columns,\n",
    "    entitlements=entitlements\n",
    ")\n",
    "\n",
    "datagrid_id = datagrid.save()\n",
    "datagrid.initialize()\n",
    "datagrid.poll()\n",
    "datagrid.to_frame()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Once we have our Datagrid we can use it as our data source to create the visualizations. Let's start with a Bar Chart\n",
    "example:\n",
    "\n",
    "### Bar Charts\n",
    "\n",
    "To create a bar chart, simply pass in your datagrid and entitlements to the `PlotlyViz` class to initialize the class."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization = PlotlyViz(datagrid=datagrid, entitlements=entitlements)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Then, call the bar function, passing the parameters that you require for your visualization. In this case we're simply\n",
    "passing x, y and title. We're passing the column names from our Datagrid to our `x` and `y` parameters as well as a \n",
    "visualization title:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization.bar(x='Name', y='Realized Vol', title=\"Realized Vol Bar Chart Example\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "*Note: All parameters used in our visualization functions are the same parameters you'd use in [Plotly Express](https://plotly.com/python/plotly-express/).\n",
    "\n",
    "Next, we can persist our chart and visualize it in Marquee like so:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "viz_id = visualization.save()\n",
    "# The open() method does not work in Jupyter, but it works fine from your own IDE.\n",
    "visualization.open() \n",
    "print(f'Your data visualization should be viewable at: https://marquee.gs.com/s/markets/visualizations/{viz_id}') \n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Scatter Charts\n",
    "\n",
    "Similarly, we can create scatter charts like so:\n",
    "\n",
    "In our previous Bar chart example, we passed the Datagrid variable directly to `PlotlyViz`. Now, since that Datagrid\n",
    "is already created, let's simply use it's ID as reference:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization = PlotlyViz(datagrid_id=datagrid_id, entitlements=entitlements)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Next, use the scatter function and pass the required variables. After that, you should be able to save and open it to\n",
    "visualize in Marquee:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization.scatter(x='Implied Volatility', y='Realized Vol', color=\"Name\", title=\"Scatter Chart Example\")\n",
    "viz_id = visualization.save()\n",
    "# The open() method does not work in Jupyter, but it works fine from your own IDE.\n",
    "visualization.open()\n",
    "print(f'Your data visualization should be viewable at: https://marquee.gs.com/s/markets/visualizations/{viz_id}') "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Bubble Charts\n",
    "\n",
    "Bubble charts are actually scatter charts but with a third variable that specifies the dot size. Here's an example:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization = PlotlyViz(datagrid_id=datagrid_id, entitlements=entitlements)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now, call the scatter function with the required variables. After that, you should be able to save and open it to\n",
    "visualize in Marquee:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Using Realized Vol as the column to use for dot size\n",
    "visualization.scatter(x='Implied Volatility', y='Realized Vol', size='Realized Vol', color=\"Name\", title=\"Bubble Chart Example\")\n",
    "viz_id = visualization.save()\n",
    "# The open() method does not work in Jupyter, but it works fine from your own IDE.\n",
    "visualization.open()\n",
    "print(f'Your data visualization should be viewable at: https://marquee.gs.com/s/markets/visualizations/{viz_id}') "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Radar Charts\n",
    "\n",
    "A useful chart in different scenarios is the Radar Chart. This chart can be created like so:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization = PlotlyViz(datagrid_id=datagrid_id, entitlements=entitlements)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "The function to call this time is called `line_polar`. Call this function with the required variables. After that, you should be able to save and open it to\n",
    "visualize in Marquee:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "visualization.line_polar(r='Implied Volatility', theta=\"Name\", line_close=True, title=\"Radar Chart\")\n",
    "viz_id = visualization.save()\n",
    "# The open() method does not work in Jupyter, but it works fine from your own IDE.\n",
    "visualization.open()\n",
    "print(f'Your data visualization should be viewable at: https://marquee.gs.com/s/markets/visualizations/{viz_id}') "
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Again, all the parameters used in these visualizations are the same that [Plotly Express](https://plotly.com/python/plotly-express/) uses,\n",
    "You can also see some useful examples on their site for the visualizations we support: [Bar](https://plotly.com/python/bar-charts/), \n",
    "[Scatter](https://plotly.com/python/line-and-scatter/), [Bubble](https://plotly.com/python/line-and-scatter/#bubble-scatter-plots), \n",
    "[Pie](https://plotly.com/python/pie-charts/), [Radar](https://plotly.com/python/radar-chart/)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}